package iss.java;

import iss.java.list.MyList;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * Created by wenke on 2016/9/16.
 */
public class Main_B {
    public static void main(String[] args) {
        // TODO: Write a multithreaded testcase against requirement B.
        // NOTE: Testcase for requirement C is not required.

        Lock lock = new ReentrantLock();
        MyList list=new MyList();
        Thread ta=new Thread()
        {
          public void run()
          {
              for(int i=0; i<100; i++)
              {
                  lock.lock();
                  try
                  {
                      list.insert(list.getHead(), i);
                      System.out.println("a nod with data "+i+" is inserted.");
                  }
                  finally
                  {
                      lock.unlock();
                  }
              }
          }
        };
        Thread tb=new Thread()
        {
            public void run()
            {
                for(int i=0; i<100; i++)
                {
                    lock.lock();
                    try
                    {
                        System.out.println("a node with data "+list.getHead().getNext().getData()+" is removed,");
                        list.remove(list.getHead().getNext());
                    }
                    finally
                    {
                        lock.unlock();
                    }
                }
            }
        };
        ta.start();
        tb.start();
    }
}
